package cn.fastoo.web.api.controller.user;

import cn.fastoo.common.dao.UserAccountDao;
import cn.fastoo.common.dao.UserBaseDao;
import cn.fastoo.common.dao.UserSmstemplateDao;
import cn.fastoo.common.entity.UserAccount;
import cn.fastoo.common.entity.UserBase;
import cn.fastoo.common.entity.UserSmstemplate;
import cn.fastoo.common.sampleframe.SFBeanUtils;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.utils.Constant;
import cn.fastoo.service.UserService;
import cn.fastoo.web.dto.UserAccountDto;
import cn.fastoo.web.dto.UserTemplateDto;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 签名
 * @author daniel.chan
 */
@Controller
@RequestMapping("/v1/admin")
public class TemplateController {

	private final Logger logger = LoggerFactory.getLogger(TemplateController.class);

	@Autowired
	private UserSmstemplateDao userSmstemplateDao;

	/**
	 * 列表
	 */
	@RequestMapping(value = "template")
	@ResponseBody
	public String templateUI(UserBase userBase,String searchApiKey,String keyword,Integer pageNo){
		Search search = new Search();
		search.addSearchFilter("EQ_userBaseId",userBase.getId());
		if (StringUtils.isNotBlank(searchApiKey)) {
			search.addSearchFilter("EQ_apiKey",searchApiKey);
		}

		if (StringUtils.isNotBlank(keyword)) {
			search.addSearchFilter("LIKE_templateContent",keyword);
		}
		pageNo = pageNo == null ? 0 : pageNo;
		Page<UserSmstemplate> page = userSmstemplateDao.findPage(search.addPageRequest(new PageRequest(pageNo,10,new Sort(Sort.Direction.DESC,"id"))));

		JSONObject jsonObject = new JSONObject();
		//包装userSmstemplates数据
		List<UserTemplateDto> userTemplateDtos = new ArrayList<UserTemplateDto>();
		List<UserSmstemplate> ls = page.getContent();
		for (UserSmstemplate ua:ls) {
			userTemplateDtos.add(SFBeanUtils.injectByObject(new UserTemplateDto(),ua));
		}
		jsonObject.put("userSmstemplates",userTemplateDtos);
		jsonObject.put("totalElements",page.getTotalElements());
		jsonObject.put("size",page.getSize());
		jsonObject.put("totalPages",page.getTotalPages());

		return new JsonResult(0,"ok",jsonObject).toJson(SerializerFeature.WriteMapNullValue);
	}

	/**
	 * 申请模板
	 */
	@RequestMapping(value = "template/save")
	@ResponseBody
	public String templateSave(UserBase userBase,Long templateId,@RequestParam String selectApiKey,@RequestParam Integer templateType,@RequestParam String templateContent,String appUrl){
		UserSmstemplate userSmstemplate =null;
		if (templateId!=null) {
			userSmstemplate = userSmstemplateDao.findById(templateId);
		}
		if (userSmstemplate == null) {
			userSmstemplate = new UserSmstemplate();
			userSmstemplate.setUserBaseId(userBase.getId());
			userSmstemplate.setApplyTime(new Date());
			userSmstemplate.setApiKey(selectApiKey);
			userSmstemplate.setAppUrl(appUrl);
			userSmstemplate.setTemplateContent(templateContent);
			userSmstemplate.setTemplateType(templateType);
			userSmstemplateDao.insert(userSmstemplate);
		} else {
			userSmstemplate.setApiKey(selectApiKey);
			userSmstemplate.setTemplateContent(templateContent);
			userSmstemplate.setTemplateType(templateType);
			userSmstemplate.setAppUrl(appUrl);
			userSmstemplate.setState(Constant.TemplateStatus.UnSign.getValue());
			userSmstemplateDao.update(userSmstemplate);
		}
		return JsonResult.success("保存成功");
	}


	/**
	 * 删除模板
	 */
	@RequestMapping(value = "template/del")
	@ResponseBody
	public String deltemplate(@RequestParam Long templateId) {
		try {
			userSmstemplateDao.delete(templateId);
			return JsonResult.success("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JsonResult.create(101,"删除失败");
	}


}
